package com.erp.erp_servers.RoleGroup.imp;

import com.erp.erp_dao.RoleGroup.IRoleGroupDAO;
import com.erp.erp_entitys.Role.RoleEntity;
import com.erp.erp_entitys.RoleGroup.RoleGroup2Entity;
import com.erp.erp_entitys.RoleGroup.RoleGroupEntity;
import com.erp.erp_entitys.RoleGroup.RoleGroupEntityExample;
import com.erp.erp_entitys.RoleGroupMember.RoleGroupMemberEntity;
import com.erp.erp_servers.Role.IRoleService;
import com.erp.erp_servers.RoleGroup.IRoleGroupService;
import com.erp.erp_servers.RoleGroupMember.IRoleGroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImpRoleGroupService
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-07-04 11:03
 * @Version 1.0
 */
@Service
public class ImpRoleGroupService implements IRoleGroupService {
    @Autowired
    IRoleGroupDAO roleGroupDAO;
    @Autowired
    IRoleGroupMemberService roleGroupMemberService;
    @Autowired
    IRoleService roleService;
    /**
     * 新增
     *
     * @param record
     * @return
     */
    @Override
    public int insertSelective(RoleGroupEntity record) {
        return roleGroupDAO.insertSelective(record);
    }

    /**
     * 根据条件查询
     *
     * @param record
     * @return
     */
    @Override
    public List<RoleGroupEntity> selectByExample(RoleGroupEntity record) {

        return roleGroupDAO.selectByExample(this.getExample(record));
    }

    @Override
    public List<RoleGroupEntity> selectByExample(RoleGroupEntityExample example) {
        return roleGroupDAO.selectByExample(example);
    }

    /**
     * 根据条件更新
     *
     * @param record
     * @return
     */
    @Override
    public int updateByExampleSelective(RoleGroupEntity record) {
        return roleGroupDAO.updateByExampleSelective(record,this.getExample(record));
    }

    /**
     * 根据条件删除
     *
     * @param record
     * @return
     */
    @Override
    public int deleteByExample(RoleGroupEntity record) {
        return roleGroupDAO.deleteByExample(this.getExample(record));
    }

    @Override
    public int deleteByExample(RoleGroupEntityExample example) {
        return roleGroupDAO.deleteByExample(example);
    }

    /**
     * 获取角色树
     *
     * @return
     */
    @Override
    public List<RoleGroup2Entity> getRoleTree() {
        //获取角色组列表
        RoleGroupEntity record=new RoleGroupEntity();
        List<RoleGroupEntity> roleGroupEntities = roleGroupDAO.selectByExample(this.getExample(record));

        //获取角色组成员列表
        RoleGroupMemberEntity record2=new RoleGroupMemberEntity();
        List<RoleGroupMemberEntity> roleGroupMemberEntities = roleGroupMemberService.selectByExample(record2);
        //以角色组code为key，以rolecode列表为值，构建map
        Map<String,List<String>> roleGroupMemberMap=new HashMap<String,List<String>>();
        for(int i=0;i<roleGroupMemberEntities.size();i++){
            String roleGroupCode=roleGroupMemberEntities.get(i).getGroupCode();
            if(roleGroupMemberMap.containsKey(roleGroupCode)){
                List<String> list=roleGroupMemberMap.get(roleGroupCode);
                list.add(roleGroupMemberEntities.get(i).getRoleCode());
            }else{
                List<String> list=new ArrayList<String>();
                list.add(roleGroupMemberEntities.get(i).getRoleCode());
                roleGroupMemberMap.put(roleGroupCode,list);
            }
        }
        //获取所有的角色
        RoleEntity record3=new RoleEntity();
        List<RoleEntity> roleEntities = roleService.selectByExample(record3);
        //以rolecode为key，以角色为值，构建map
        Map<String,RoleEntity> roleMap=new HashMap<String, RoleEntity>();
        for(int i=0;i<roleEntities.size();i++){
            roleMap.put(roleEntities.get(i).getCode(),roleEntities.get(i));
        }
        //组装角色树
        List<RoleGroup2Entity> list=new ArrayList<RoleGroup2Entity>();
        for(int i=0;i<roleGroupEntities.size();i++){//遍历角色组列表
            RoleGroupEntity roleGroupEntity = roleGroupEntities.get(i);
            String code = roleGroupEntity.getCode();//角色组code
            //构建角色树
            RoleGroup2Entity roleGroup2Entity=new RoleGroup2Entity();
            roleGroup2Entity.setId(roleGroupEntity.getId());
            roleGroup2Entity.setName(roleGroupEntity.getName());
            roleGroup2Entity.setDesc(roleGroupEntity.getDesc());
            roleGroup2Entity.setCode(roleGroupEntity.getCode());

            List<RoleGroup2Entity> roles=new ArrayList<RoleGroup2Entity>();
            if(roleGroupMemberMap.containsKey(code)){//遍历角色组成员Map,取到组内角色code列表
                List<String> strings = roleGroupMemberMap.get(code);
                for(int n=0;n<strings.size();n++){
                    RoleGroup2Entity roleEntity = this.conver(roleMap.get(strings.get(n)));
                    roles.add(roleEntity);
                }
                roleGroup2Entity.setRoles(roles);
            }
            list.add(roleGroup2Entity);
        }
        return list;
    }
private RoleGroup2Entity conver(RoleEntity re){
    RoleGroup2Entity rge=new RoleGroup2Entity();
    rge.setCode(re.getCode());
    rge.setName(re.getName());
    rge.setId(re.getId());

        return rge;
}
    /**
     * 根据实体类获取条件对象RoleGroupEntityExample
     * @param record
     * @return
     */
    private RoleGroupEntityExample getExample (RoleGroupEntity record){
        RoleGroupEntityExample example=new RoleGroupEntityExample();
        RoleGroupEntityExample.Criteria criteria = example.createCriteria();
        if(record.getId() !=null){
            criteria.andIdEqualTo(record.getId());
        }
        if(record.getCode() !=null && record.getCode() !=""){
           // criteria.andCodeEqualTo(record.getCode());
            criteria.andCodeLike("%"+record.getCode()+"%");
        }
        if(record.getName() !=null && record.getName() !=""){
            //criteria.andNameEqualTo(record.getName());
            criteria.andNameLike("%"+record.getName()+"%");
        }
        return example;
    }
}
